package xyz.erupt.flow.service.impl;

import com.github.pagehelper.util.StringUtil;
import lombok.extern.slf4j.Slf4j;
import lombok.var;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import xyz.erupt.core.exception.EruptApiErrorTip;
import xyz.erupt.flow.bean.entity.OaWorkFlowDefinition;
import xyz.erupt.flow.bean.entity.OaWorkFlowGroup;
import xyz.erupt.flow.repository.OaWorkFlowDefinitionRepository;
import xyz.erupt.flow.repository.OaWorkFlowGroupRepository;
import xyz.erupt.flow.service.WorkFlowGroupService;

import javax.persistence.criteria.Join;
import java.time.LocalDateTime;
import java.util.*;
import java.util.stream.Collectors;

/**
 * @description: 工作流定义组
 * @className: WorkFlowGroupServiceImpl
 * @author: PP
 * @date: 2024/10/19 20:44
 * @Version: 1.0
 */
@Service
@Slf4j
public class WorkFlowGroupServiceImpl implements WorkFlowGroupService {
    @Autowired
    private OaWorkFlowGroupRepository workFlowGroupRepository;
    @Autowired
    private OaWorkFlowDefinitionRepository workFlowDefinitionRepository;

    @Override
    public List<OaWorkFlowGroup> getGroups(String keyword) {
        List<OaWorkFlowGroup> groups = workFlowGroupRepository.findAllByKeyword(keyword);
        //末尾添加两个分组
        groups.add(OaWorkFlowGroup.builder()
                .groupId(0L)
                .groupName("其他")
                .sort(999)
                .updated(new Date(System.currentTimeMillis()))
                .build());

        groups.add(OaWorkFlowGroup.builder()
                .groupId(-1L)
                .groupName("已停用")
                .sort(9999)
                .updated(new Date(System.currentTimeMillis()))
                .build());
        //添加流程定义数据
        groups.forEach(s->{
            var new_list =new ArrayList<OaWorkFlowDefinition>();
            var list = workFlowDefinitionRepository.listByGroupId(s.getGroupId(),keyword);
            list.forEach(d->{
                new_list.add(OaWorkFlowDefinition.builder().id(d.getId()).formName(d.getFormName()).updateTime(d.getUpdateTime()).stop(d.isStop())
                                .processKey(d.getProcessKey())
                        .remark(d.getRemark()).logo(d.getLogo()).workFlowGroup(
                                OaWorkFlowGroup.builder()
                                .groupId(s.getGroupId())
                                        .groupName(d.getWorkFlowGroup() == null?"其他":d.getWorkFlowGroup().getGroupName()==null?"其他":d.getWorkFlowGroup().getGroupName())
                                        .build())
                        .build()
                );
            });
            s.setWorkFlowDefinitions(new_list);

        });
        return groups;
    }

    @Override
    public void GroupsSort(List<Long> groups) {
        List<OaWorkFlowGroup> updateList = new ArrayList<>();
        for (int i1 = 0;i1<groups.size();i1++){
            OaWorkFlowGroup data = workFlowGroupRepository.findById(groups.get(i1)).orElse(null);
            data.setSort(i1);
            updateList.add(data);
        }
        workFlowGroupRepository.saveAll(updateList);
    }

    @Override
    public void updateGroupName(Long id, String name) {
        OaWorkFlowGroup workFlowGroup = workFlowGroupRepository.findById(id).orElseThrow(()->new EruptApiErrorTip("未发现组"));
        workFlowGroup.setGroupName(name);
        workFlowGroupRepository.save(workFlowGroup);
    }

    @Override
    public void createGroup(String name) {
        workFlowGroupRepository.save(OaWorkFlowGroup.builder().sort(1).groupName(name).updated(new Date(System.currentTimeMillis())).build());
    }

    @Override
    public void deleteGroup(Long id) {
        workFlowGroupRepository.deleteById(id);
    }
}
